package com.qf.shopping.Realm;

import com.qf.shopping.mapper.IAdminMangerMapper;
import com.qf.shopping.pojo.System.Menu;
import com.qf.shopping.pojo.System.Role;
import com.qf.shopping.pojo.System.User;
import com.qf.shopping.service.AdminMangerService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.util.*;

public class MyRealm extends AuthorizingRealm {


    @Autowired
    private IAdminMangerMapper adminMangerMapper=null;
    @Override
    public String getName() {
        return "MyRealm";
    }
//认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //首先获取用户名
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
        //获取前段传过来的用户名和密码
        String username=token.getUsername();
        String password=String.valueOf(token.getPassword());


        //根据用户名获取用户的对象
        User user=adminMangerMapper.findUserByUserName(username);

        if(user==null){
            throw new AccountException("用户名不正确");
        }else if (!password.equals(user.getPassword())){
            throw new AccountException("密码不正确");
        }else {
            return new SimpleAuthenticationInfo(user, password, getName());
        }

    }


    // 权限的方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("进入权限认证。。");

        Subject subject = SecurityUtils.getSubject();
        User user1 =(User) subject.getPrincipal();
        String username=user1.getUserName();


        //通过用户名获取角色
         User user =adminMangerMapper.findUserByUserName(username);
        List<Role> roleslist=adminMangerMapper.findRolesByUserName(user.getUserName());
        Set<String> role=new HashSet<>();
        String role1 =null;
        for (Role r:roleslist) {
            role1=r.getName();
            role.add(r.getName());
        }
        System.out.println(role);
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo(role);
        //通过角色获取权限
        List<Menu> list =adminMangerMapper.findMenuByRoleName(role1);
        List<String> menulist=new ArrayList<>();
        for (Menu m:list) {
            menulist.add(m.getMenuName());
        }
        System.out.println(menulist);
        authorizationInfo.addStringPermissions(menulist);
        System.out.println("当前用户的权限："+menulist);
        return authorizationInfo;

    }

    @Bean
    public MyRealm shiroRealm(){
        MyRealm shiroRealm = new MyRealm();
        shiroRealm.setCachingEnabled(true);
        //启用身份验证缓存，即缓存AuthenticationInfo信息，默认false
        shiroRealm.setAuthenticationCachingEnabled(true);
        //缓存AuthenticationInfo信息的缓存名称 在ehcache-shiro.xml中有对应缓存的配置
        shiroRealm.setAuthenticationCacheName("authenticationCache");
        //启用授权缓存，即缓存AuthorizationInfo信息，默认false
        shiroRealm.setAuthorizationCachingEnabled(true);
        //缓存AuthorizationInfo信息的缓存名称  在ehcache-shiro.xml中有对应缓存的配置
        shiroRealm.setAuthorizationCacheName("authorizationCache");
        return shiroRealm;
    }

    /**
     * 重写方法,清除当前用户的的 授权缓存
     * @param principals
     */
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    /**
     * 重写方法，清除当前用户的 认证缓存
     * @param principals
     */
    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

//    @Override
//    public void clearCache(PrincipalCollection principals) {
//        super.clearCache(principals);
//    }
//
//    /**
//     * 自定义方法：清除所有 授权缓存
//     */
//    public void clearAllCachedAuthorizationInfo() {
//        getAuthorizationCache().clear();
//    }
//
//    /**
//     * 自定义方法：清除所有 认证缓存
//     */
//    public void clearAllCachedAuthenticationInfo() {
//        getAuthenticationCache().clear();
//    }
//
//    /**
//     * 自定义方法：清除所有的  认证缓存  和 授权缓存
//     */
//    public void clearAllCache() {
//        clearAllCachedAuthenticationInfo();
//        clearAllCachedAuthorizationInfo();
//    }
}
